#include <cstdio>

int main( )
{
    int x[ 8 ], t, i, p, mx, c, d, ans, sum;
    bool flag;
    scanf("%d", &t);
    while ( t-- )
    {
        mx = 0;
        for ( i = 0; i < 8; i++ )
            scanf("%d", &x[ i ]);
        scanf("%d", &sum);
        flag = true;
        d = 0;
        ans = 0;
        p = 1;
        while ( flag )
        {
            flag = false;
            c = 0;
            for ( i = 0; i < 8; i++ )
            {
                if ( x[ i ] ) flag = true;
                c += x[ i ] % 2;
                x[ i ] >>= 1;
            }
            if ( c & 1 )
            {
                if ( ( sum + d ) & 1 )
                    d = ( d + c ) / 2;
                else
                {
                    d = ( 8 - c + d ) / 2;
                    ans += p;
                }
            }
            else
            {
                if ( ( sum + d ) & 1 )
                {
                    d = ( 8 - c + d ) / 2;
                    ans += p;
                }
                else
                    d = ( d + c ) / 2;
            }
            p <<= 1;
            sum >>= 1;
            if ( d || sum ) flag = true;
        }
        printf("%d\n", ans);
    }
    return 0;
}
